package com.example.administrator.xiaoxiang.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.example.administrator.xiaoxiang.R;
import com.example.administrator.xiaoxiang.activity.MovieActivity;
import com.example.administrator.xiaoxiang.utils.AsyncTaskHelper;

/**
 * Created by Administrator on 2017/1/10.
 */

public class HomeSlideImage extends ImageView implements AsyncTaskHelper.OnDataDownloadListener{
    private static final String TAG = "HomeSlideImage";
    private float cx = 68, cy = 247+63;//图片的位置
    private int width=150,height=150;
    private float old_cx,old_cy;//初始位置
    private Paint paint;
    AsyncTaskHelper helper;
    private Bitmap bitmap;
    private long newTime;
    private long oldTime;

    public HomeSlideImage(Context context) {
        this(context,null);
    }

    public HomeSlideImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init(){
        paint = new Paint();
        paint.setAntiAlias(true);
        helper = new AsyncTaskHelper();
        bitmap  = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        helper.downloadData("http://img.xiaoxiangyoupin.com/image/150x150_642204492757995520_fixed.png",this);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap,cx-width/2,cy-height/2,paint);
        old_cx = cx;
        old_cy = cy;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        cx = event.getX();
        cy = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (cx > old_cx - width/2 && cx < width/2 + old_cx && cy > old_cy-height/2 && cy < old_cy+height/2) {
                    oldTime = System.currentTimeMillis();
                    cx = event.getX();
                    cy = event.getY();
                    return true;
                } else{
                    cx = old_cx;
                    cy = old_cy;
                    return false;
                }
            case MotionEvent.ACTION_MOVE:
                cx = event.getX();
                cy = event.getY();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                newTime = System.currentTimeMillis();
                Log.e(TAG, "onTouchEvent: "+(newTime - oldTime)+"xx:\n"+(cx-old_cx)+"yy:"+(cy-old_cy) );
                if (newTime - oldTime <100) {
                    Intent intent = new Intent(getContext(), MovieActivity.class);
                     getContext().startActivity(intent);
                    break;
                }
                if (event.getX()+width/2 <= getWidth() / 2) {
                    cx = width/2;
                    cy = event.getY()+height/2;
                }
                if (event.getX()+width/2 > getWidth() / 2) {
                    cx = getWidth() - width/2;
                    cy = event.getY()+height/2;
                }
                if (event.getY() <= 0)
                {
                    cy = height/2;
                }
                if (event.getY()+height/2>=getHeight()-20){
                    cy = getHeight()-height-20+height/2;
                }
                invalidate();
                break;
        }
        return true;
    }

    @Override
    public void onDataDownload(byte[] result) {
        bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
        width = bitmap.getWidth()-14;
        height = bitmap.getHeight()-23;
        invalidate();
    }

}
